package com.ruike.alisurface.ui.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ruike.alisurface.R;
import com.ruike.alisurface.bean.SlotLocationBean;
import com.voodoo.lib_logcat.L;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * 货道位置详情适配器
 */
public class LocalReplenAdapter extends BaseQuickAdapter<SlotLocationBean.DataBean.SlotPosBean, BaseViewHolder> implements LoadMoreModule {

    Context mContext;

    public LocalReplenAdapter(@Nullable List<SlotLocationBean.DataBean.SlotPosBean> data, Context context) {
        super(R.layout.dialog_replenishment_item, data);
        this.mContext = context;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, SlotLocationBean.DataBean.SlotPosBean dataBean) {
        helper.setText(R.id.item_location, dataBean.getPosition_index() + "");
        helper.setText(R.id.item_status, dataBean.getPosition_state() == 1 ? "有货" : "缺货");

        helper.setText(R.id.item_valid_period, dataBean.getWarranty_time_format());

        ImageView deltime = helper.getView(R.id.item_del_period);
        if (!TextUtils.isEmpty(dataBean.getWarranty_time_format())) {
            deltime.setVisibility(View.VISIBLE);
        } else {
            deltime.setVisibility(View.GONE);
        }

        EditText batch_number = helper.getView(R.id.item_batch_number);
        EditText terilization_number = helper.getView(R.id.item_sterilization_number);

        batch_number.clearFocus();
        if (batch_number.getTag() != null && batch_number.getTag() instanceof TextWatcher) {
            batch_number.removeTextChangedListener((TextWatcher) batch_number.getTag());
        }
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String txt = batch_number.getText().toString();
                if (!TextUtils.isEmpty(txt) && !TextUtils.equals(String.valueOf(dataBean.getBatch_no()), txt)) {
                    dataBean.setBatch_no(txt);
                }

            }
        };
        batch_number.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    batch_number.addTextChangedListener(textWatcher);
                }
            }
        });
        batch_number.setTag(textWatcher);
        batch_number.setText(dataBean.getBatch_no() + "");


        terilization_number.clearFocus();

        if (terilization_number.getTag() != null && terilization_number.getTag() instanceof TextWatcher) {
            terilization_number.removeTextChangedListener((TextWatcher) terilization_number.getTag());

        }
        TextWatcher textWatcher2 = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String txt = terilization_number.getText().toString();
                if (!TextUtils.isEmpty(txt) && !TextUtils.equals(String.valueOf(dataBean.getSter_no()), txt)) {
                    dataBean.setSter_no(txt);
                }
            }
        };

        terilization_number.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    terilization_number.addTextChangedListener(textWatcher2);
                }
            }
        });
        terilization_number.setTag(textWatcher2);
        terilization_number.setText(dataBean.getSter_no() + "");

        int postion = helper.getLayoutPosition();
        if (postion == 0) {
            helper.getView(R.id.item_move_up).setVisibility(View.INVISIBLE);
            helper.getView(R.id.item_move_down).setVisibility(View.VISIBLE);
        } else if (postion == getData().size() - 1) {
            helper.getView(R.id.item_move_down).setVisibility(View.INVISIBLE);
            helper.getView(R.id.item_move_up).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.item_move_down).setVisibility(View.VISIBLE);
            helper.getView(R.id.item_move_up).setVisibility(View.VISIBLE);
        }
    }
}
